import { PremiumResource, Resource, ResourceStatus } from './resource.model'
import { ChildEntity, Column, Entity, JoinTable, ManyToMany, PrimaryColumn } from 'typeorm'
import { User } from '@boticord/users-client'
import { BotLibrary, BotTags } from '@boticord/consts'
import { RatingAmongCount } from '@boticord/common'
import { NotifySettings } from './notify-settings.model'

@ChildEntity()
export class Bot extends Resource {
  @Column()
  declare prefix: string

  @Column()
  declare discriminator: string

  @Column('timestamp with time zone', { nullable: false, default: () => 'CURRENT_TIMESTAMP' })
  declare createdDate: Date

  @Column({ default: null, nullable: true })
  declare supportServerInviteCode?: string

  @ManyToMany(() => User)
  @JoinTable()
  declare developers: User[]

  @Column({
    type: 'enum',
    enum: BotLibrary,
    nullable: true,
    default: null,
  })
  declare library?: BotLibrary

  // bot guilds count
  @Column({ nullable: true, default: null })
  declare guilds?: number

  // bot members count
  @Column({ default: null, nullable: true })
  declare members?: number

  // bot shards count
  @Column({ default: null, nullable: true })
  declare shards?: number

  @Column({ nullable: true, default: null })
  declare website?: string

  // from Resource
  declare tags: BotTags[]
}

/**
 * pending bot is a bot that is waiting for approval, almost all data is the same as bot, but it's not public,
 * and it's not in the main database, it's just a temporary model used to avoid situations
 * when bot isn't approved, and someone not owner of the bot is trying to add it, so real owner can
 * add bot to the monitoring, and then it will be approved without conflicts
 * */
@Entity()
export class PendingBot {
  @PrimaryColumn({ unique: true })
  declare id: string // id is a combination of botID and userID

  @Column()
  declare botID: string

  @Column()
  declare name: string

  @Column()
  declare shortDescription: string

  @Column()
  declare description: string

  @Column({ nullable: true, default: null, select: false })
  declare comment?: string

  @Column()
  declare systemApproved: boolean

  @Column({ nullable: true, default: null })
  declare avatar?: string

  @Column({ nullable: true, default: null })
  declare shortLink?: string

  @Column()
  declare owner: string

  @Column({ default: ResourceStatus.Pending })
  declare status: ResourceStatus.Pending | ResourceStatus.Banned

  @Column()
  declare prefix: string

  @Column(() => NotifySettings)
  declare notify: NotifySettings

  @Column()
  declare discriminator: string

  @Column('timestamp with time zone', { nullable: false, default: () => 'CURRENT_TIMESTAMP' })
  declare createdDate: Date

  @Column({ default: null, nullable: true })
  declare inviteLink: string

  @Column({ default: null, nullable: true })
  declare supportServerInviteCode?: string

  @ManyToMany(() => User)
  @JoinTable()
  declare developers: User[]

  @Column({
    type: 'enum',
    enum: BotLibrary,
    nullable: true,
    default: null,
  })
  declare library?: BotLibrary

  @Column({ nullable: true, default: null })
  declare website?: string

  @Column({ default: 0 })
  declare standardBannerID: number

  @Column('int', { array: true, default: [] })
  declare tags: BotTags[]

  @Column({ type: 'jsonb' })
  declare ratings: RatingAmongCount[]

  @Column(() => PremiumResource)
  declare premium: PremiumResource
}
